local M = _G.ServerOHOH.class.new(script,_G.ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);
local ServerOHOH = _G.ServerOHOH;
--local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent());
local CarTool=_G.CommonOHOH.load.CarTool();
function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
    self.player=self.parent
	self.ItemCtrl=self.parent:GetComponent(ServerOHOH.load.Server_ItemCtrl())
	self.DataBase=self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())
    self.skinList = {}
	print("初始化--- 车辆部件仓库-->")
end;
--装扮卸载
function M.SkinUnEquip(self,carId,carCfg)

	if self.skinList[carCfg.type]  then
		local  removeId = carId
		assert( removeId,"removeId==null")
		local skinData =self.DataBase:GetData("car").skin.__real
		local indexToRemove = nil
		for index, value in pairs(skinData) do
			if index == removeId then
				indexToRemove = index
				break
			end
		end
		if indexToRemove then
			skinData[indexToRemove]=nil
			--table.remove(self.DataBase:GetData("car").skin.__real, indexToRemove)
			--print("table.remove",carId,skinData)
			self.skinList[carCfg.type]= nil
		end
		

		self.DataBase:Execut(function()
		end);
		self.parent:SynchDataToClient("car")
		--self.DecoEuqipUpdateEvent:Action()
		--self:ChangeRngData(carCfg,-1)
		return true
	end
end

function M.SkinMakeUp(self,carId)
	assert(carId,"carId==null")
	local carCfg = CarTool.GetPartCfgData(carId)
--读取skin数据
	local skindata = self.DataBase:GetData("car").skin.__real
	if skindata then
		for carId, status in pairs(skindata) do
			local type = CarTool.GetPartCfgData(carId).type
			self.skinList[type] = carId
		end
	end
--皮肤点位上有物品则需要先销毁
	if self.skinList[carCfg.type]  then
		local oldCfg = CarTool.GetPartCfgData(self.skinList[carCfg.type])
		self:SkinUnEquip(self.skinList[carCfg.type],oldCfg)
	end

	self.skinList[carCfg.type]= carId
--添加新物体
	skindata[carId] = 1
	for key, value in pairs(skindata) do
		skindata[key] = 1
 	end

	self.DataBase:Execut(function()
	end);
	self.parent:SynchDataToClient("car")	
    return true

end;
function M.NewMakeUp(self)
--读取skin数据
	local skindata = self.DataBase:GetData("car").skin.__real
	local equipdata = self.DataBase:GetData("car").equip.__real
	if skindata then
		for carId, status in pairs(skindata) do
			local oldCfg = CarTool.GetPartCfgData(carId)
			self:SkinUnEquip(carId,oldCfg)
		end
 
		if equipdata  then
			for i = 1, #equipdata do
				local type = CarTool.GetPartCfgData(equipdata[i]).type
				self.skinList[type] = skindata[i]
				skindata[equipdata[i]] = 0
			end
			self.DataBase:Execut(function()
			end);
			self.parent:SynchDataToClient("car")	
   			return true
		end
	end

	
	
end;

local function_list = ServerOHOH.function_list;

M:RegistFunction(function_list.skin_makeup,M.SkinMakeUp)
M:RegistFunction(function_list.new_makeup,M.NewMakeUp)
return M
